package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BookDetailsDTO;
import model.UserDTO;
import DAO.BookDetailsDAO;
import DAO.DAOFactory;
import DAO.NotFoundException;
import controller.LoginServlet;

/**
 * Servlet implementation class BookDetailsController
 */
@WebServlet("/BookDetailsController")
public class BookDetailsController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookDetailsController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest req, HttpServletResponse res) throws NotFoundException {
		
		//to get session
		UserDTO user = (UserDTO)req.getSession().getAttribute("LoggedinUser");
		int userId = user.getUserId();
		//System.out.println(req.getSession());
		//req.getSession().setAttribute("user", user);
		
		
		ArrayList<BookDetailsDTO> table1;
		BookDetailsDAO b = DAOFactory.loadInstance().getBookDetailsDAO();
		
//		String pathInfo=req.getQueryString(req.getPathInfo());
//		System.out.println(pathInfo);
//		int pos = pathInfo.indexOf("=");
//        String selection = pathInfo.substring(pos+1,pos+5);
//        System.out.println("Sevlet command: " + selection);
//		HttpSession session=req.getSession();
//		session.setAttribute("bookingID", selection);
		
		table1 = b.findAwaitingApprovalByUserID(userId);
		req.setAttribute("booking", table1);
		//req.getSession().setAttribute("facilities", table);
		RequestDispatcher rd = req.getRequestDispatcher("BookingInProcess.jsp");
		try {
			rd.forward(req, res);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
	}
